<template>
  <div class="login-container">
    <!-- 主内容区 -->
    <div class="login-wrapper">
      <!-- 左侧信息区 -->
      <div class="info-panel">
        <div class="brand">
          <i class="fa fa-shield"></i>
          <h1>SecureSystem</h1>
        </div>
        
        <div class="features">
          <div class="feature-item">
            <div class="feature-icon">
              <i class="fa fa-lock"></i>
            </div>
            <div class="feature-text">
              <h3>数据加密</h3>
              <p>所有信息均经过加密处理，保障您的隐私安全</p>
            </div>
          </div>
          
          <div class="feature-item">
            <div class="feature-icon">
              <i class="fa fa-bolt"></i>
            </div>
            <div class="feature-text">
              <h3>快速访问</h3>
              <p>优化的认证流程，减少等待时间</p>
            </div>
          </div>
          
          <div class="feature-item">
            <div class="feature-icon">
              <i class="fa fa-globe"></i>
            </div>
            <div class="feature-text">
              <h3>多端同步</h3>
              <p>一次登录，多设备无缝切换</p>
            </div>
          </div>
        </div>
        
        <div class="copyright">
          &copy; 2023 SecureSystem. 保留所有权利
        </div>
      </div>
      
      <!-- 右侧登录表单区 -->
      <div class="form-panel">
        <div class="form-header">
          <h2>账户登录</h2>
          <p>请输入您的账号信息以继续</p>
        </div>
        
        <form @submit.prevent="handleLogin" class="login-form">
          <!-- 用户名/邮箱输入 -->
          <div class="form-group">
            <label for="username">用户名/邮箱</label>
            <div class="input-wrapper">
              <i class="fa fa-user"></i>
              <input
                type="text"
                id="username"
                v-model="form.username"
                @blur="validateField('username')"
                :class="{'valid': formValid.username && form.username, 'invalid': !formValid.username && touched.username}"
                placeholder="请输入用户名或邮箱"
              >
              <span v-if="touched.username" class="validation-icon">
                <i v-if="formValid.username && form.username" class="fa fa-check"></i>
                <i v-else-if="!formValid.username && form.username" class="fa fa-times"></i>
              </span>
            </div>
            <p v-if="!formValid.username && touched.username && form.username" class="error-message">
              请输入有效的用户名或邮箱
            </p>
          </div>
          
          <!-- 密码输入 -->
          <div class="form-group">
            <div class="form-label-wrapper">
              <label for="password">密码</label>
              <a href="#" class="forgot-link">忘记密码?</a>
            </div>
            <div class="input-wrapper">
              <i class="fa fa-lock"></i>
              <input
                :type="showPassword ? 'text' : 'password'"
                id="password"
                v-model="form.password"
                @blur="validateField('password')"
                :class="{'valid': formValid.password && form.password, 'invalid': !formValid.password && touched.password}"
                placeholder="请输入密码"
              >
              <button
                type="button"
                class="toggle-password"
                @click="showPassword = !showPassword"
              >
                <i v-if="showPassword" class="fa fa-eye-slash"></i>
                <i v-else class="fa fa-eye"></i>
              </button>
              <span v-if="touched.password" class="validation-icon">
                <i v-if="formValid.password && form.password" class="fa fa-check"></i>
                <i v-else-if="!formValid.password && form.password" class="fa fa-times"></i>
              </span>
            </div>
            <p v-if="!formValid.password && touched.password && form.password" class="error-message">
              密码长度至少为6个字符
            </p>
          </div>
          
          <!-- 记住我选项 -->
          <div class="remember-option">
            <input
              type="checkbox"
              id="remember"
              v-model="form.remember"
            >
            <label for="remember">记住我 30 天</label>
          </div>
          
          <!-- 登录按钮 -->
          <button
            type="submit"
            class="login-button"
            :disabled="isSubmitting || !isFormValid"
          >
            <span v-if="!isSubmitting">登录</span>
            <span v-else>
              <i class="fa fa-spinner fa-spin"></i> 登录中...
            </span>
          </button>
        </form>
        
        <!-- 其他登录方式 -->
        <div class="other-login-options">
          <div class="divider">
            <span>或使用以下方式登录</span>
          </div>
          
          <div class="social-login">
            <button class="social-btn google">
              <i class="fa fa-google"></i>
            </button>
            <button class="social-btn facebook">
              <i class="fa fa-facebook"></i>
            </button>
            <button class="social-btn wechat">
              <i class="fa fa-weixin"></i>
            </button>
          </div>
        </div>
        
        <!-- 注册提示 -->
        <div class="register-prompt">
          还没有账户? <a href="/register">立即注册</a>
        </div>
      </div>
    </div>
    
    <!-- 登录成功弹窗 -->
    <div v-if="showSuccessModal" class="success-modal">
      <div class="modal-content">
        <div class="success-icon">
          <i class="fa fa-check"></i>
        </div>
        <h3>登录成功!</h3>
        <p>正在为您跳转至主页...</p>
        <div class="progress-bar">
          <div class="progress" :style="{ width: progress + '%' }"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';

// 表单数据
const form = ref({
  username: '',
  password: '',
  remember: false
});

// 表单状态跟踪
const touched = ref({
  username: false,
  password: false
});

// 控制密码显示/隐藏
const showPassword = ref(false);

// 提交状态
const isSubmitting = ref(false);

// 成功弹窗状态
const showSuccessModal = ref(false);
const progress = ref(0);

// 路由实例
const router = useRouter();

// 表单验证规则
const formValid = computed(() => ({
  username: form.value.username.trim() !== '' && 
            (form.value.username.includes('@') ? 
             /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.value.username) : 
             form.value.username.length >= 3),
  password: form.value.password.length >= 6
}));

// 整体表单是否有效
const isFormValid = computed(() => {
  return formValid.value.username && formValid.value.password;
});

// 验证单个字段
const validateField = (field) => {
  touched.value[field] = true;
};

// 处理登录提交
const handleLogin = async () => {
  // 标记所有字段为已触碰
  touched.value.username = true;
  touched.value.password = true;
  
  // 检查表单是否有效
  if (!isFormValid.value) return;
  
  // 模拟登录过程
  isSubmitting.value = true;
  
  try {
    // 模拟API请求
    await new Promise(resolve => setTimeout(resolve, 1500));
    
    // 显示成功弹窗
    showSuccessModal.value = true;
    
    // 进度条动画
    const interval = setInterval(() => {
      progress.value += 5;
      if (progress.value >= 100) {
        clearInterval(interval);
        // 跳转至主页
        setTimeout(() => {
          router.push('/dashboard');
        }, 500);
      }
    }, 80);
    
  } catch (error) {
    console.error('登录失败:', error);
    alert('登录失败，请检查您的账号信息');
  } finally {
    isSubmitting.value = false;
  }
};
</script>

<style scoped>
/* 基础样式 */
.login-container {
  min-height: 100vh;
  background-color: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-wrapper {
  width: 100%;
  max-width: 1000px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
}

/* 左侧信息面板 */
.info-panel {
  flex: 1;
  background: linear-gradient(135deg, #3b82f6, #10b981);
  color: #fff;
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
}

.brand {
  display: flex;
  align-items: center;
  margin-bottom: 60px;
}

.brand i {
  font-size: 32px;
  margin-right: 15px;
}

.brand h1 {
  font-size: 28px;
  font-weight: 700;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 40px;
  margin-bottom: auto;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  gap: 20px;
  opacity: 0.9;
  transition: all 0.3s ease;
}

.feature-item:hover {
  opacity: 1;
  transform: translateX(5px);
}

.feature-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.feature-icon i {
  font-size: 20px;
}

.feature-text h3 {
  font-size: 18px;
  margin-bottom: 8px;
  font-weight: 600;
}

.feature-text p {
  font-size: 14px;
  opacity: 0.8;
  line-height: 1.5;
}

.copyright {
  margin-top: 60px;
  font-size: 14px;
  opacity: 0.8;
}

/* 右侧表单面板 */
.form-panel {
  flex: 1;
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
}

.form-header {
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 26px;
  font-weight: 700;
  color: #1e293b;
  margin-bottom: 8px;
}

.form-header p {
  color: #64748b;
  font-size: 14px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-size: 14px;
  font-weight: 500;
  color: #334155;
}

.form-label-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.forgot-link {
  font-size: 13px;
  color: #3b82f6;
  text-decoration: none;
  transition: color 0.2s ease;
}

.forgot-link:hover {
  color: #2563eb;
}

.input-wrapper {
  position: relative;
}

.input-wrapper input {
  width: 100%;
  padding: 14px 16px;
  padding-left: 45px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font-size: 15px;
  transition: all 0.2s ease;
}

.input-wrapper input:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.input-wrapper i:first-child {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
}

.toggle-password {
  position: absolute;
  right: 40px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  transition: color 0.2s ease;
}

.toggle-password:hover {
  color: #64748b;
}

.validation-icon {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
}

.validation-icon .fa-check {
  color: #10b981;
}

.validation-icon .fa-times {
  color: #ef4444;
}

.error-message {
  font-size: 13px;
  color: #ef4444;
  margin-top: 2px;
}

.input-wrapper input.valid {
  border-color: #10b981;
}

.input-wrapper input.invalid {
  border-color: #ef4444;
}

.remember-option {
  display: flex;
  align-items: center;
  gap: 10px;
}

.remember-option input {
  width: 16px;
  height: 16px;
  accent-color: #3b82f6;
}

.remember-option label {
  font-size: 14px;
  color: #64748b;
  cursor: pointer;
}

.login-button {
  padding: 14px;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.login-button:hover {
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
}

.login-button:disabled {
  background: #94a3b8;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* 其他登录方式 */
.other-login-options {
  margin-top: 40px;
}

.divider {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 30px;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #e2e8f0;
}

.divider span {
  font-size: 13px;
  color: #94a3b8;
}

.social-login {
  display: flex;
  gap: 15px;
  justify-content: center;
}

.social-btn {
  width: 45px;
  height: 45px;
  border-radius: 50%;
  border: 1px solid #e2e8f0;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.social-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.social-btn.google i {
  color: #ea4335;
}

.social-btn.facebook i {
  color: #1877f2;
}

.social-btn.wechat i {
  color: #07c160;
}

/* 注册提示 */
.register-prompt {
  margin-top: 40px;
  text-align: center;
  font-size: 14px;
  color: #64748b;
}

.register-prompt a {
  color: #3b82f6;
  font-weight: 500;
  text-decoration: none;
  transition: color 0.2s ease;
}

.register-prompt a:hover {
  color: #2563eb;
}

/* 成功弹窗 */
.success-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

.modal-content {
  background: white;
  padding: 30px;
  border-radius: 12px;
  width: 100%;
  max-width: 350px;
  text-align: center;
  animation: scaleIn 0.3s ease;
}

.success-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: #dcfce7;
  color: #16a34a;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
}

.success-icon i {
  font-size: 30px;
}

.modal-content h3 {
  font-size: 20px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 10px;
}

.modal-content p {
  color: #64748b;
  font-size: 14px;
  margin-bottom: 20px;
}

.progress-bar {
  height: 6px;
  background: #e2e8f0;
  border-radius: 3px;
  overflow: hidden;
}

.progress {
  height: 100%;
  background: #10b981;
  transition: width 0.3s ease;
}

/* 动画 */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes scaleIn {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
  }
  
  .info-panel {
    display: none;
  }
  
  .form-panel {
    padding: 40px 20px;
  }
  
  .form-header {
    text-align: center;
    margin-bottom: 30px;
  }
}
</style>
